package com.it.mydream.Controller;

import com.it.mydream.Bean.Rank;
import com.it.mydream.Bean.Seed;
import com.it.mydream.Bean.User;
import com.it.mydream.Service.imp.LoginServiceImp;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DaTingController {

    int size = 2;

    @Autowired
    private LoginServiceImp loginService;

    @RequestMapping("goDaTing")
    public String goDaTing(HttpServletRequest request){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20009");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        List<User> userlist = loginService.getUserList();
        application.setAttribute("userlist",userlist);
        request.setAttribute("user",user);

        Map<String, Seed> seedlist = (Map<String, Seed>) application.getAttribute("seedlist");
        if(seedlist==null){
            seedlist = new HashMap<>();
            application.setAttribute("seedlist",seedlist);
        }

        return "other/dating";
    }

    @RequestMapping("findDTList")
    @ResponseBody
    public List<Map<String, Object>> find(HttpServletRequest request) {
        ServletContext application = request.getServletContext();
        List<Map<String, Object>> list ;
        if (application.getAttribute("DTList") == null) {
            list = new ArrayList<>();
            for(int i = 0; i <size; i++){
                Map<String, Object> map = new HashMap<>();
                map.put("user1",null);
                map.put("user2",null);
                map.put("seed",null);
                list.add(map);
            }
        } else {
            list = (List<Map<String, Object>>) application.getAttribute("DTList");
            for (int i = 0; i < size; i++) {
                Map<String, Object> map = list.get(i);
                map = new HashMap<>(map);
                String seed = (String) map.get("seed");
                User user1 = (User) map.get("user1");
                User user2 = (User) map.get("user2");
                if(seed != null && user1==null && user2 == null){
                    map.put("seed",null);
                    list.set(i,map);
                }
            }
        }
        application.setAttribute("DTList", list);
        return list;
    }

    @RequestMapping("joinDTList")
    @ResponseBody
    public void join(HttpServletRequest request) {
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();

        String s = request.getParameter("s");
        int n = Integer.parseInt(request.getParameter("n"));
        User user = (User) session.getAttribute("user");
        List<Map<String, Object>> list = (List<Map<String, Object>>) application.getAttribute("DTList");
        for (int i = 0; i < size; i++) {
            Map<String, Object> map = list.get(i);
            User user1 = (User) map.get("user1");
            User user2 = (User) map.get("user2");
            if(user1 != null&&user1.getUser_id()==user.getUser_id()){
                map = new HashMap<>(map);
                map.put("user1",null);
                list.set(i, map);
                break;
            }
            if(user2 != null&&user2.getUser_id()==user.getUser_id()){
                map = new HashMap<>(map);
                map.put("user2",null);
                list.set(i, map);
                break;
            }
        }
        if(s.equals("0")){
            Map<String, Object> map = list.get(n);
            if(map.get("user1")==null){
                map = new HashMap<>(map);
                map.put("user1",user);
            }else{
                map = new HashMap<>(map);
                map.put("user2",user);
            }
            list.set(n, map);
        }
        application.setAttribute("DTList", list);
    }

}
